package nobnak.study.gl.projection;

import nobnak.study.gl.projection.RenderTask.PostProgressListener;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class StudyProjectionActivity extends Activity {
	public static final String LOG_TAG = "StudyProjection";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
//      final GLSurfaceView glView = (GLSurfaceView) findViewById(R.id.myGLSurfaceView1);
//		MyGLRenderer renderer = new MyGLRenderer();
//		glView.setRenderer(renderer);
        
        final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
		
		float[] modelWorldMatrix = new float[Constant.MATRIX_4_BY_4];
		MatrixMath.setIdentityMatrix(modelWorldMatrix, 0);
		Bitmap[] textures = new Bitmap[] {
			((BitmapDrawable)getResources().getDrawable(R.drawable.dice1)).getBitmap(),
			((BitmapDrawable)getResources().getDrawable(R.drawable.dice2)).getBitmap(),
			((BitmapDrawable)getResources().getDrawable(R.drawable.dice3)).getBitmap(),
			((BitmapDrawable)getResources().getDrawable(R.drawable.dice4)).getBitmap(),
			((BitmapDrawable)getResources().getDrawable(R.drawable.dice5)).getBitmap(),
			((BitmapDrawable)getResources().getDrawable(R.drawable.dice6)).getBitmap()
		};
		Cube cube = new Cube(modelWorldMatrix, textures);
		final Triangle[] triangles = cube.tessellate();
		
		final RenderTask task = new RenderTask(new PostProgressListener() {
			@Override
			public void onPostProgress(Bitmap result) {
				imageView.setImageBitmap(result);
			}
		});
		final int width = 100;
		final int height = 100;
		final float near = 1.0f;
		final float far = 20f;
		final float fovy = 45;
		
		final Button buttonFront = (Button) findViewById(R.id.button1);
		final Button buttonBack = (Button) findViewById(R.id.button2);
		final StudyProjectionActivity activity = this;
		buttonFront.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				float[] eye = new float[] {3, 3, 3};
				float[] lookAt = new float[] {0, 0, 0};
				float[] vpvMatrix = TriangleUtil.makeRasterMatrix(width, height, near, far, fovy, eye, lookAt);
				task.execute(width, height, vpvMatrix, triangles, activity);
			}
		});
		buttonBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				float[] eye = new float[] {-3, 3, -3};
				float[] lookAt = new float[] {0, 0, 0};
				float[] vpvMatrix = TriangleUtil.makeRasterMatrix(width, height, near, far, fovy, eye, lookAt);
				task.execute(width, height, vpvMatrix, triangles, activity);
			}
		});
		
		Log.d(LOG_TAG, "test");
    }


}